function cropped_im = CropImage (im, P)

x_vec = P(1,:);
y_vec = P(2,:);

w1 = sqrt((y_vec(1)-y_vec(2))^2+(x_vec(1)-x_vec(2))^2);
w2 = sqrt((y_vec(3)-y_vec(4))^2+(x_vec(3)-x_vec(4))^2);

h1 = sqrt((y_vec(2)-y_vec(3))^2+(x_vec(2)-x_vec(3))^2);
h2 = sqrt((y_vec(4)-y_vec(1))^2+(x_vec(4)-x_vec(1))^2);

w = max([w1,w2]);
h = max([h1,h2]);

cropped_im = zeros(h, w);
dest_pts = [0, w, w, 0; 0, 0, h, h];

H = CalculateHomography3x3 (P, dest_pts);